package com.zxx.view;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.soufang.ui.R;
import com.zxx.adapter.*;
import com.zxx.common.Common;

/**
 * @brief spinner控件更多视图
 */
public class ViewMore extends LinearLayout implements ViewBaseAction {
	
	private ListView regionListView;
	private ListView plateListView;
	private ArrayList<String> groups = new ArrayList<String>();
	private LinkedList<String> childrenItem = new LinkedList<String>();
	private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>();
	private TextAdapter plateListViewAdapter;
	private TextAdapter earaListViewAdapter;
	private OnSelectListener mOnSelectListener;
	private int leftPosition = 0;
	private int rightPosition = 0; 
	private String showString;
	private static final int RightSortKey =0;
	private static final int RightRoomKey =1;
	private static final int RightZXKey =2;
	private static final int RightPriceKey =3;
	
	 private int[] viewLeftRow = new int[] {
			  R.string.sort,
			  R.string.buildType,R.string.zx,R.string.perprice
	  };
	
	 private int[] viewRightSort = new int[] {
			  R.string.origion,
			  R.string.sortByPriceH_L,R.string.sortByPriceL_H,R.string.sortByYearH_L,
			  R.string.sortByYearL_H,R.string.sortByAreaH_L,R.string.sortByAreaL_H
	  };
	 
	 private int[] viewRightRoom = new int[] {
			  R.string.oneRoom,
			  R.string.twoRoom,R.string.threeRoom
	  };
	
	
	
	 private int[] viewRightPrice = new int[] {
			  R.string.below1w,
			  R.string.between1w_2w,R.string.above2w
	  };
	
	public ViewMore(Context context) {
		super(context);
		init(context);
	}

	public ViewMore(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_region, this, true);
		regionListView = (ListView) findViewById(R.id.listView);
		plateListView = (ListView) findViewById(R.id.listView2);
		setBackgroundDrawable(getResources().getDrawable(
				R.drawable.choosearea_bg_left));

		initChildData();

		earaListViewAdapter = new TextAdapter(context, groups,
				R.drawable.choose_item_selected,
				R.drawable.choose_eara_item_selector);
		earaListViewAdapter.setTextSize(17);
		earaListViewAdapter.setSelectedPositionNoNotify(leftPosition);
		regionListView.setAdapter(earaListViewAdapter);
		earaListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, int position) {
						if (position < children.size()) {
							childrenItem.clear();
							childrenItem.addAll(children.get(position));
							plateListViewAdapter.notifyDataSetChanged();
							leftPosition = position;
						}
					}
				});
		if (leftPosition < children.size())
			childrenItem.addAll(children.get(leftPosition));
		plateListViewAdapter = new TextAdapter(context, childrenItem,
				R.drawable.choose_item_right,
				R.drawable.choose_plate_item_selector);
		plateListViewAdapter.setTextSize(15);
		plateListViewAdapter.setSelectedPositionNoNotify(rightPosition);
		plateListView.setAdapter(plateListViewAdapter);
		plateListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, final int position) {
						
						showString = childrenItem.get(position);
						if (mOnSelectListener != null) {
							rightPosition = position;
							mOnSelectListener.getValue(leftPosition,rightPosition,showString);
						}

					}
				});
		
		setDefaultSelect();
	}

	public void setDefaultSelect() {
		regionListView.setSelection(0);
		plateListView.setSelection(0);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		public void getValue(int leftPosition,int rightposition,String showText);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
	/**
	 * 初始化双列表的所有数据
	 */
	private void initChildData()
	{
		Resources r = getResources();
		showString = r.getString(R.string.more);
		for(int i=0;i<viewLeftRow.length;i++){
			groups.add(i,r.getString(viewLeftRow[i]));
			LinkedList<String> tItem = new LinkedList<String>();
			switch(i)
			{
				case RightSortKey:
				{
					for(int j=0;j<viewRightSort.length;++j)
					{
						tItem.add(j,r.getString(viewRightSort[j]));
					}
				}
				break;
				case RightRoomKey:
				{
					for(int j=0;j<viewRightRoom.length;++j)
					{
						tItem.add(j,r.getString(viewRightRoom[j]));
					}
				}
					break;
				case RightZXKey:
				{
					for(int j=0;j<Common.GetLengOFZX();++j)
					{
						int resID = Common.GetZXid(j);
						tItem.add(j,r.getString(resID));
					}
				}
					break;
				case RightPriceKey:
				{
					for(int j=0;j<viewRightPrice.length;++j)
					{
						tItem.add(j,r.getString(viewRightPrice[j]));
					}
				}
					break;
				default:
					break;
			}
			children.put(i, tItem);
		}
	}
}
